<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Repositories\OtherOrderRepository;
use App\Repositories\UserRepository;
use App\Http\Requests\OtherOrderRequest;
use Carbon\Carbon;

class OtherOrderController extends AdminController
{
    public function __construct(
        OtherOrderRepository $otherOrder, 
        UserRepository $user
    )
    {
        $this->otherOrder = $otherOrder;
        $this->user = $user;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $condition = $request->except('page');
        $where = collect($condition);

        // dd($where);

        $designer = $this->user->designerSelect()->prepend('请选择', 0);
        $data = $this->otherOrder->paginateList($where);

        return view('admin.other_order.index', compact('where', 'designer', 'data'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $designer = $this->user->designerSelect()->prepend('选择设计师', 0);

        return view('admin.other_order.create', compact('designer'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(OtherOrderRequest $request)
    {
        $request->request->add(['started_at' => Carbon::now()->format('Y-m-d')]);

        $result = $this->otherOrder->create($request->all());

        $referer = $request->input('referer');
        $goto = $referer ? redirect($referer) : back();

        if ($result) {
            return $goto->with('success', '您已经成功下单');
        }

        return $goto->with('error', '对不起，您的订单添加失败');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $designer = $this->user->designerSelect();
        $data = $this->otherOrder->find($id);

        return view('admin.other_order.edit', compact('data', 'designer'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        if (!$request->has('programmer_id')) {
            $request->request->add(['programmer_id'=>0]);
        }
        $result = $this->otherOrder->updateFind($request->all(), $id);

        $referer = $request->input('referer');
        $goto = $referer ? redirect($referer) : back();

        if ($result) {
            return $goto->with('success', '其他订单更新成功');
        }

        return $goto->with('error', '其他订单更新失败');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $result = $this->otherOrder->delete($id);

        if ($result) {
            flash('success', '其他订单删除成功');
            return response()->json(['code' => 200]);
        }

        flash('error', '其他订单删除失败');
        return response()->json(['code' => 500]);
    }
}
